package com.efast.cafe.portal.security.service;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.common.PortalUserDetails;
import com.efast.cafe.portal.bean.common.PortalUserSecurityBean;
import com.efast.cafe.portal.entity.company.PortalCompanyUser;
import com.efast.cafe.portal.service.company.IPortalCompanyUserService;
/**
 * 
    * @ClassName: MyAuthenticationManager
    * @Description: 
	 * 实现spring security 中的UserDetailsService接口
	 * UserDetailsService接口用于返回用户相关数据。
	 * 它有loadUserByUsername()方法，根据username查询用户实体，可以实现该接口覆盖该方法，
	 * 实现自定义获取用户过程。该接口实现类被DaoAuthenticationProvider 类使用，用于认证过程中载入用户信息。
    * @author xuanming.feng
    * @date 2018年8月3日
    *
 */
@Service
public class MyAuthenticationManager implements UserDetailsService{
	
	@Autowired
	private IPortalCompanyUserService portalCompanyUserService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
		String uc[] = username.split(CommonConstant.J_COMPANY_USERNAME_SEPARATOR);
		String companyCode = uc[0];
		username = uc[1];
		
		PortalCompanyUser user = null;
		PortalUserSecurityBean bean = new PortalUserSecurityBean();
		
		try{
			user = portalCompanyUserService.userLogin(companyCode, username, null);
			
			if(uc.length==3){// 免密码
				
				if(user != null){
					String pwd = uc[2];
					BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
					
					user.setCompany_code(companyCode);
					user.setPwd(passwordEncoder.encode(pwd));
				}
				
			}
		}
		catch(ServiceException excep){
			
			if(uc.length==3){// 免密码
				
				user = new PortalCompanyUser();
				String pwd = uc[2];
				BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
				
				user.setCompany_code(companyCode);
				user.setPwd(passwordEncoder.encode(pwd));
			}
			else
			{
				throw new UsernameNotFoundException(excep.getMessage());
			}
		}
		
		bean.setCompany_code(user.getCompany_code());
		bean.setEmail(user.getEmail());
		bean.setPhone(user.getPhone());
		bean.setType(user.getType());
		bean.setDefault_user(user.getDefault_user());
		bean.setUser_level(user.getUser_level());
		bean.setSex(user.getSex());
		bean.setReal_name(user.getReal_name());
		bean.setPwd(user.getPwd());
		bean.setUsername(companyCode+"_"+user.getUsername());
		bean.setPortal_company_user_id(user.getPortal_company_user_id());
		bean.setUname(user.getUsername());
		Set<GrantedAuthority> authorities = new HashSet<>();
        return new PortalUserDetails(
        		companyCode+"_"+username, user.getPwd(),
                true,//是否可用
                true,//是否过期
                true,//证书不过期为true
                true,//账户未锁定为true
                authorities,bean);
	}
	
	

}
